﻿using PayPal.Api;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2.Models
{
    public class PaypalService
    {
        private APIContext apiContext;

        public PaypalService()
        {
            this.apiContext = new APIContext(PaypalConfig.authTokenCredential().GetAccessToken());
            this.apiContext.Config = PaypalConfig.paypalSdkConfig();
        }
        public Payment createPayment(
            Double total,
            String currency,
            PaypalPaymentMethod method,
            PaypalPaymentIntent intent,
            String description,
            String cancelUrl,
            String successUrl) 
        {
            Amount amount = new Amount();
        amount.currency=currency;
        amount.total="0.01";

        Transaction transaction = new Transaction();
        transaction.description=description;
        transaction.amount=amount;

        List<Transaction> transactions = new List<Transaction>();
        transactions.Add(transaction);

        Payer payer = new Payer();
        payer.payment_method=method.ToString();

        Payment payment = new Payment();
        payment.intent=intent.ToString();
        payment.payer=payer;
        payment.transactions=transactions;
        RedirectUrls redirectUrls = new RedirectUrls();
        redirectUrls.cancel_url=cancelUrl;
        redirectUrls.return_url=successUrl;
        payment.redirect_urls=redirectUrls;

        return payment.Create(apiContext);
    }

    public Payment executePayment(String paymentId, String payerId)
    {
        Payment payment = new Payment();
        payment.id=paymentId;
        PaymentExecution paymentExecute = new PaymentExecution();
        paymentExecute.payer_id=payerId;
        return payment.Execute(apiContext, paymentExecute);
    }
    }
}